package cndw.core.dispatch;

import java.util.Collection;
import java.util.Iterator;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cndw.core.dispatch.exception.ActionFilterException;
import cndw.core.util.LogConst;

/**
 *
 * @author zuojie.x
 */
final class ActionFilterExecutor {
	private final Logger log = LoggerFactory.getLogger(LogConst.CORE);
	
	private Collection<ActionFilter> filters;
	
	ActionFilterExecutor(Collection<ActionFilter> filters) {
		this.filters = filters;
	}
	
	void beforeAction(Request request) throws ActionFilterException {
		// 控制器过滤器
		ActionFilter filter = null;
		try {
			for (Iterator<ActionFilter> iter = filters.iterator(); iter.hasNext();) {
				filter = iter.next();
				filter.beforeAction(request);
			}
		} catch (Exception ae) {
			log.error("ActionFilter:{}出现异常.", filter);
		}
	}
	
	void afterAction(Request request, Response response) throws ActionFilterException {
		// action执行后过滤器
		ActionFilter filter = null;
		try {
			for (Iterator<ActionFilter> iter = filters.iterator(); iter.hasNext();) {
				filter = iter.next();
				filter.afterAction(request, response);
			}
		} catch (Exception ae) {
			log.error("ActionFilter:{}出现异常.", filter);
		}
	}
}
